
#include "tools/log/log_core.h"


#include <iostream>
using namespace std;

namespace tools{

    namespace{

        typedef LogConfig::Str Str;

        Str Deep(const size_t d,const Str::value_type ch=' ') { return Str(d*4,ch); }
    }
    
    LogCore::LogCore()
        :mCurrent(nullptr)
    {}

    void LogCore::AddLine(const Str& line)
    {
        if(!mCurrent)
            if( mSections.empty() )
                mSections.emplace_back("begin-section"),
                mCurrent = &mSections.back();
        mCurrent->AddLine(line);
    }
    void LogCore::OpenSection(const Str& name)
    {
        if(!mCurrent)
            mSections.emplace_back(name),
            mCurrent = &mSections.back();
        else
            mCurrent = mCurrent->AddSection(name);

            //mSections.emplace_back(name, *mCurrent);
        
    }
    void LogCore::CloseSection()
    {
        if( !mCurrent || mSections.empty() )
            return;
        mCurrent = mCurrent->mParent;
    }


    void ViewNameSection(const Section& sec, const size_t d)
    {
        cout<< Deep(d)<< sec.mName <<endl;
        for(const auto& rec: sec.mContent)
            if(rec.mSection)
                ViewNameSection(*rec.mSection, d+1);
    }
    void ViewSection(const Section& sec, const size_t d)
    {
        cout<< Deep(d)<< sec.mName <<endl;
        for(const auto& rec: sec.mContent)
            if(rec.mSection)
                ViewSection(*rec.mSection, d+1);
            else
                cout<< Deep(d+1)<< rec.mLine<<endl;
    }


    void LogCore::ViewTree()const
    {
        cout<<" ------- TREE -------\n";

        for(const auto& sec: mSections)
            ViewNameSection(sec,0);
    }
    void LogCore::ViewContent()const
    {
        cout<<" ------- CONTENT -------\n";

        for(const auto& sec: mSections)
            ViewSection(sec,0);

        
    }


    
}//namespace tools



